package grpcUtils

import (
	"context"
	"github.com/labstack/gommon/log"
	"google.golang.org/grpc"
	"time"
)

func UnaryInterceptor() grpc.UnaryServerInterceptor {
	return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
		now := time.Now()
		defer func() {
			if recoverError := recover(); recoverError != nil {
				log.Error(now.Format(time.DateTime), "method:", info.FullMethod, "\t recover:", recoverError)
			} else {
				log.Info(now.Format(time.DateTime), "method:", info.FullMethod, "\t req:", req)
			}
		}()
		resp, err = handler(ctx, req)
		return resp, err
	}

}
